﻿using MediatR;
using S.Produce.Api.Application.Commands.WearningCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.WearningCommandHandlers
{
    /// <summary>
    /// 删除断奶信息
    /// </summary>
    public class WeaningDeleteCommandHandler : IRequestHandler<WeaningDeleteCommand, int>
    {
        private readonly IRepository<Weanings> repository;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="repository"></param>
        public WeaningDeleteCommandHandler(IRepository<Weanings> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Handle(WeaningDeleteCommand request, CancellationToken cancellationToken)
        {
            var ids = request.Ids.Split(",");
            var count = 0;
            foreach (var item in ids)
            {
                var del = await repository.GetAsync(int.Parse(item));
                del.IsDel = true;
                count += await repository.UpdateAsync(del);
            }
            return count;
        }
    }
}
